package cst.a931xmc.yuejian20.Activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.squareup.picasso.Picasso;

import cst.a931xmc.yuejian20.Bean.TrainBean;
import cst.a931xmc.yuejian20.Bean.TrainOrderBean;
import cst.a931xmc.yuejian20.Listener.Listener;
import cst.a931xmc.yuejian20.Model.ApiModel;
import cst.a931xmc.yuejian20.Model.TrainModel;
import cst.a931xmc.yuejian20.R;

public class TorderActivity extends AppCompatActivity implements View.OnClickListener {
    private Context context;
    private int torder_id;
    private LinearLayout train_item;
    private ImageView train_pic;
    private TextView train_name;
    private TextView train_price;
    private TextView train_intro;
    private TextView torder_num;
    private TextView torder_price;
    private TextView torder_rname;
    private TextView torder_tel;
    private TextView torder_time;
    private TextView torder_orderid;
    private ImageButton btn_back;
    private TrainModel model;
    private Listener<TrainOrderBean> listener;
    private Listener<TrainBean> listener1;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_trainorder);
        init();
        initData();
        model = new TrainModel();
        model.getOrderDetail(torder_id,listener);
    }

    public void init(){
        train_item = findViewById(R.id.train_item);
        train_pic = findViewById(R.id.train_pic);
        train_name = findViewById(R.id.train_name);
        train_price = findViewById(R.id.train_price);
        train_intro = findViewById(R.id.train_intro);
        torder_num = findViewById(R.id.torder_num);
        torder_price = findViewById(R.id.torder_price);
        torder_rname = findViewById(R.id.torder_rname);
        torder_tel = findViewById(R.id.torder_tel);
        torder_time = findViewById(R.id.torder_time);
        torder_orderid = findViewById(R.id.torder_orderid);
        btn_back = findViewById(R.id.btn_back);
        btn_back.setOnClickListener(this);
        Intent intent = getIntent();
        torder_id = intent.getExtras().getInt("torder_id");
    }

    public void initData(){
        listener = new Listener<TrainOrderBean>() {
            @Override
            public void onResponse(TrainOrderBean l) {
                if(l.getTorder_id()!=0){
                    torder_num.setText("x" + l.getTorder_num());
                    torder_price.setText("￥" + l.getTorder_price());
                    torder_rname.setText(l.getTorder_rname());
                    torder_tel.setText(l.getTorder_tel());
                    torder_time.setText(l.getTorder_time());
                    torder_orderid.setText(l.getTorder_orderid());
                    model.getDetail(l.getTrain_id(),listener1);
                }
                else {
                    Toast.makeText(TorderActivity.this,"加载失败",Toast.LENGTH_LONG).show();
                }
            }
            @Override
            public void onFail(String message) {
                Toast.makeText(TorderActivity.this,message, Toast.LENGTH_LONG).show();
            }
        };

        listener1 = new Listener<TrainBean>() {
            @Override
            public void onResponse(final TrainBean l) {
                if(l.getTrain_id()!=0){
                    train_name.setText(l.getTrain_name());
                    train_price.setText("￥" + l.getTrain_price());
                    train_intro.setText(l.getTrain_intro());
                    ApiModel apiModel = new ApiModel();
                    Picasso.with(context)
                            .load(apiModel.getBaseUrl() + "static/images/" + l.getTrain_pic())
                            .placeholder(R.drawable.nopic)
                            .into(train_pic);
                    train_item.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            Intent intent = new Intent(TorderActivity.this,TrainActivity.class);
                            intent.putExtra("train_id",l.getTrain_id());
                            startActivity(intent);
                            finish();
                        }
                    });
                }
                else {
                    Toast.makeText(TorderActivity.this,"加载失败",Toast.LENGTH_LONG).show();
                }
            }

            @Override
            public void onFail(String message) {
                Toast.makeText(TorderActivity.this,message, Toast.LENGTH_LONG).show();
            }
        };
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_back:
                finish();
                break;
        }
    }
}
